namespace Kazoo
{
    using GotXtraCC;
    using Mischief;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Net;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;
    using Yahoo;
    using Yahoo.Ymsg;
    using System.Security.Permissions;

    public class Form2 : Form
    {
        private ToolStripPanel BottomToolStripPanel;
        private Button button1;
        private Button button2;
        private Button button3;
        private Button button4;
        private Button button5;
        private Button Hook;
        private YmsgClient client;
        private ColumnHeader columnHeader1;
        private IContainer components = null;
        private ToolStripContentPanel ContentPanel;
        private ContextMenuStrip contextMenuStrip1;
        private Userlist ds;
        private Userlist dss;
        private Tablist dss2;
        private Userlist dssd;
        private Userlist dssd3;
        private Tablist level2; 
        private Tablist level3;
        private ButtonGrey bgrey;

        private ToolStripMenuItem ignoreToolStripMenuItem;
        public bool inroom;
        private int Joinattempts;
        private bool keyed;
        private ToolStripPanel LeftToolStripPanel;
        private Tablist level;
        private ListView listView1;
        private ToolStripPanel RightToolStripPanel;
        private StatusStrip statusStrip1;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private TabPage tabPage2;
        private TextBox textBox1;
        private TextBox textBox2;
        private TextBox textBox3;
        private Timer timer1;
        private ToolStripStatusLabel toolStripStatusLabel1;
        private ToolStripPanel TopToolStripPanel;
        private ToolStripMenuItem unignoreToolStripMenuItem;
        private VoiceStream wd;
        private XtraProgressBar xtraProgressBar1;
        public VoiceStream.KazooListHandler AddUser;
        public VoiceStream.KazooListHandler DelUser;
        private string globaluser;
        public Form2()
        {
            InitializeComponent();
            ds = new Userlist(adduser);
            dss = new Userlist(deluser);
            dssd = new Userlist(keyuser);
            dss2 = new Tablist(changetab);
            dssd3 = new Userlist(updateStatus);
            level = new Tablist(leveler);
            level2 = new Tablist(levelchanger);
            level3 = new Tablist(leveler3);
            bgrey = new ButtonGrey(denable);
            keyed = false;
            Joinattempts = 0;
            listView1.ItemCheck += new ItemCheckEventHandler(listView1_ItemCheck);
        }

        void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if(e.NewValue == CheckState.Checked)
            {
                wd.ignoreuser(listView1.Items[e.Index].Text);
            }
            else
            {
                wd.unignoreuser(listView1.Items[e.Index].Text);
            }
        }
        public void initvd(VoiceData vd)
        {
            //if(System.IO.File.Exists("datafile2.soap"))
            //{
            //    System.IO.FileStream stream = new System.IO.FileStream("DataFile2.soap", System.IO.FileMode.Open);
            //    SoapFormatter dsds = new SoapFormatter();
            //    VoiceData vd = (VoiceData)dsds.Deserialize(stream);
            //    stream.Close();
            //    dsds = null;
            //    System.IO.File.Delete("datafile2.soap");
                statusStrip1.Invoke(dssd3, new object[] { vd.RoomName });
                wd = new VoiceStream(vd.UserName, vd.RoomName, vd.RoomSpace, vd.VcCookie, Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
                wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
                wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
                wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
                wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
                wd.Listners1 += new VoiceStream.VCEventHandler3(vss_Listners1);
                tabControl1.Invoke(dss2, new object[] { 1 });
                
            //}
        }
        private void adduser(string data)
        {
            listView1.Items.Add(data, data, 0);
        }

void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length > 0)
            {
                client = new YmsgClient();
                client.Error += new ChatEventHandler(client_Error);
                client.VCCookie += new ChatEventHandler(client_Other);
                if (client.Login(textBox1.Text, textBox2.Text).Length > 0)
                    statusStrip1.Invoke(dssd3, new object[] { "Logged In" });
            }
            else
            {
                statusStrip1.Invoke(dssd3, new object[] { "Type A Password" });
            }
        }

        void client_Error(object sender, ChatEventArgs e)
        {
            //if(e.ID!="155")
            //statusStrip1.Invoke(dssd3, new object[] { e.ID });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        void levelchanger(int i)
        {
            if(i == 0)
            xtraProgressBar1.BarColor1 = Color.MistyRose;
        else
            xtraProgressBar1.BarColor1 = Color.WhiteSmoke;
        }

        private void button3_Click(object sender, EventArgs e)
        {

                if (!keyed)
                {
                    xtraProgressBar1.Invoke(level2, 0);
                    xtraProgressBar1.Invoke(dssd, globaluser);
                    keyed = true;
                    button3.BackColor = Color.Red;
                    wd.VoiceOut();
                }
                else
                {
                    xtraProgressBar1.Invoke(dssd, "");
                    xtraProgressBar1.Invoke(level, 0);
                    xtraProgressBar1.Invoke(level2, 1);
                    keyed = false;
                    button3.BackColor = button2.BackColor;
                    button3.Text = "Key";
                    wd.VoiceOff();
                }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Hook.Enabled = false;
            YMVCHook.PacketHook p = new YMVCHook.PacketHook();
            p.HookedRoomEvent += new YMVCHook.PacketHook.HookedRoomInfo(p_HookedEvent);
            p.start();
        }

        void p_HookedEvent(string username, string roomname, string roomspace, string roomkey)
        {
            globaluser = username;
            System.Windows.Forms.MessageBox.Show(username+"-"+roomname+"-"+roomspace+"-"+roomkey);
            Hook.Invoke(bgrey);
            statusStrip1.Invoke(dssd3, new object[] { roomname });
            globaluser = username;
            wd = new VoiceStream(username, roomname, roomspace, roomkey, Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
            wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
            wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
            wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
            wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
            wd.Listners1 += new VoiceStream.VCEventHandler3(vss_Listners1);
            wd.Connect(true);
            tabControl1.Invoke(dss2, new object[] { 1 });
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            try
            {

                if (wd != null)
                {
                    wd.vc_logout();
                    wd = null;
                }
            }
            catch (Exception e2)
            {

            }
            tabControl1.Invoke(dss2, new object[] { 0 });
            statusStrip1.Invoke(dssd3, new object[] { "Left Room" });
        }

        private void button5_Click(object sender, EventArgs e)
        {
            VoiceStream.mute = !VoiceStream.mute;
        }

        private void changetab(int i)
        {
            tabControl1.SelectTab(i);
            Joinattempts = 0;
        }

        private void client_Other(object sender, ChatEventArgs e)
        {
                Joinattempts = 0;
                timer1.Stop();
                statusStrip1.Invoke(dssd3, new object[] { client.RoomName });
            wd = new VoiceStream(client.UserName, client.RoomName, e.ID, e.Data.ToString(), Dns.Resolve("vc.yahoo.com").AddressList[0].ToString());
            wd.VCUserJoin += new VoiceStream.VCEventHandler3(wd_AddUser);
            wd.VCUserLeave += new VoiceStream.VCEventHandler3(wd_DelUser);
            wd.KeyUser += new VoiceStream.KazooListHandler(wd_KeyUser);
            wd.LevelUser += new VoiceStream.KazooListHandler1(wd_LevelUser);
            wd.Listners1 += new VoiceStream.VCEventHandler3(vss_Listners1);
            wd.Connect(true);
                tabControl1.Invoke(dss2, new object[] { 1 });
        }

        void vss_Listners1(string data)
        {
            button3.Invoke(level3, Convert.ToInt32(data));
        }

        private void deluser(string data)
        {
            listView1.Items.RemoveByKey(data);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            if (wd != null)
            {
                wd.vc_logout();
                wd = null;
            }
            base.OnClosing(e);
        }
        protected override void Dispose(bool disposing)
        {
            try
            {
                BottomToolStripPanel.Dispose();
                Hook.Dispose();
                button1.Dispose();
                button2.Dispose();
                button3.Dispose();
                button4.Dispose();
                button5.Dispose();
                client.Dispose();
                columnHeader1.Dispose();
                ContentPanel.Dispose();
                contextMenuStrip1.Dispose();
                ignoreToolStripMenuItem.Dispose();
                LeftToolStripPanel.Dispose();
                listView1.Dispose();
                RightToolStripPanel.Dispose();
                statusStrip1.Dispose();
                tabControl1.Dispose();
                tabPage1.Dispose();
                tabPage2.Dispose();
                textBox1.Dispose();
                textBox2.Dispose();
                textBox3.Dispose();
                timer1.Dispose();
                toolStripStatusLabel1.Dispose();
                TopToolStripPanel.Dispose();
                unignoreToolStripMenuItem.Dispose();
                xtraProgressBar1.Dispose();
                BottomToolStripPanel = null;
                button1 = null;
                button2 = null;
                button3 = null;
                button4 = null;
                button5 = null;
                client = null;
                columnHeader1 = null;
                ContentPanel = null;
                contextMenuStrip1 = null;
                ds = null;
                dss = null;
                dss2 = null;
                dssd = null;
                dssd3 = null;
                level3 = null;
                ignoreToolStripMenuItem = null;
                LeftToolStripPanel = null;
                level = null;
                listView1 = null;
                RightToolStripPanel = null;
                statusStrip1 = null;
                tabControl1 = null;
                tabPage1 = null;
                tabPage2 = null;
                textBox1 = null;
                textBox2 = null;
                textBox3 = null;
                timer1 = null;
                toolStripStatusLabel1 = null;
                TopToolStripPanel = null;
                unignoreToolStripMenuItem = null;
                wd = null;
                xtraProgressBar1 = null;
                AddUser = null;
                DelUser = null;
            }
            catch (Exception)
            {

            }
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            GC.Collect();
            base.Dispose(disposing);
        }

        private void ignoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.SelectedItems[0].Checked = true;
            wd.ignoreuser(listView1.SelectedItems[0].Text);
        }

        private void InitializeComponent()
        {
            components = new Container();
            Hook = new Button();
            button1 = new Button();
            button2 = new Button();
            textBox1 = new TextBox();
            textBox2 = new TextBox();
            textBox3 = new TextBox();
            contextMenuStrip1 = new ContextMenuStrip(components);
            ignoreToolStripMenuItem = new ToolStripMenuItem();
            unignoreToolStripMenuItem = new ToolStripMenuItem();
            tabControl1 = new TabControl();
            tabPage1 = new TabPage();
            statusStrip1 = new StatusStrip();
            toolStripStatusLabel1 = new ToolStripStatusLabel();
            tabPage2 = new TabPage();
            button5 = new Button();
            button4 = new Button();
            button3 = new Button();
            xtraProgressBar1 = new XtraProgressBar();
            listView1 = new ListView();
            columnHeader1 = new ColumnHeader();
            timer1 = new Timer(components);
            BottomToolStripPanel = new ToolStripPanel();
            TopToolStripPanel = new ToolStripPanel();
            RightToolStripPanel = new ToolStripPanel();
            LeftToolStripPanel = new ToolStripPanel();
            ContentPanel = new ToolStripContentPanel();
            contextMenuStrip1.SuspendLayout();
            tabControl1.SuspendLayout();
            tabPage1.SuspendLayout();
            statusStrip1.SuspendLayout();
            tabPage2.SuspendLayout();
            base.SuspendLayout();
            Hook.Location = new Point(0x16, 0x8d);
            Hook.Name = "hook";
            Hook.Size = new Size(0x7a, 0x17);
            Hook.TabIndex = 0;
            Hook.Text = "Hook Messenger";
            Hook.UseVisualStyleBackColor = true;
            Hook.Click += new EventHandler(button4_Click);
            button1.Location = new Point(0x16, 0x6d);
            button1.Name = "button1";
            button1.Size = new Size(0x2f, 0x17);
            button1.TabIndex = 0;
            button1.Text = "Login";
            button1.UseVisualStyleBackColor = true;
            button1.Click += new EventHandler(button1_Click);
            button2.Location = new Point(0x45, 0x6d);
            button2.Name = "button2";
            button2.Size = new Size(0x4b, 0x17);
            button2.TabIndex = 1;
            button2.Text = "JoinAndVc";
            button2.UseVisualStyleBackColor = true;
            button2.Click += new EventHandler(button2_Click);
            textBox1.Location = new Point(0x16, 0x19);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(0x7a, 20);
            textBox1.TabIndex = 8;
            textBox1.Text = "Username";
            textBox2.Location = new Point(0x16, 0x36);
            textBox2.Name = "textBox2";
            textBox2.Size = new Size(0x7a, 20);
            textBox2.TabIndex = 9;
            textBox2.Text = "";
            textBox2.UseSystemPasswordChar = true;
            textBox3.Location = new Point(0x16, 0x53);
            textBox3.Name = "textBox3";
            textBox3.Size = new Size(0x7a, 20);
            textBox3.TabIndex = 11;
            textBox3.Text = "The Hot Tub:1";
            contextMenuStrip1.Items.AddRange(new ToolStripItem[] { ignoreToolStripMenuItem, unignoreToolStripMenuItem });
            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new Size(0x81, 0x30);
            ignoreToolStripMenuItem.Name = "ignoreToolStripMenuItem";
            ignoreToolStripMenuItem.Size = new Size(0x80, 0x16);
            ignoreToolStripMenuItem.Text = "Ignore";
            ignoreToolStripMenuItem.Click += new EventHandler(ignoreToolStripMenuItem_Click);
            unignoreToolStripMenuItem.Name = "unignoreToolStripMenuItem";
            unignoreToolStripMenuItem.Size = new Size(0x80, 0x16);
            unignoreToolStripMenuItem.Text = "Unignore";
            unignoreToolStripMenuItem.Click += new EventHandler(unignoreToolStripMenuItem_Click);
            tabControl1.Controls.Add(tabPage1);
            tabControl1.Controls.Add(tabPage2);
            tabControl1.Location = new Point(-3, 1);
            tabControl1.Name = "tabControl1";
            tabControl1.SelectedIndex = 0;
            tabControl1.Size = new Size(0xc5, 0x108);
            tabControl1.TabIndex = 15;
            tabPage1.Controls.Add(statusStrip1);
            tabPage1.Controls.Add(button1);
            tabPage1.Controls.Add(button2);
            tabPage1.Controls.Add(Hook);
            tabPage1.Controls.Add(textBox2);
            tabPage1.Controls.Add(textBox3);
            tabPage1.Controls.Add(textBox1);
            tabPage1.Location = new Point(4, 0x16);
            tabPage1.Name = "tabPage1";
            tabPage1.Padding = new Padding(3);
            tabPage1.Size = new Size(0xbd, 0xee);
            tabPage1.TabIndex = 0;
            tabPage1.Text = "Login Info";
            tabPage1.UseVisualStyleBackColor = true;
            statusStrip1.Items.AddRange(new ToolStripItem[] { toolStripStatusLabel1 });
            statusStrip1.Location = new Point(3, 0xd5);
            statusStrip1.Name = "statusStrip1";
            statusStrip1.RenderMode = ToolStripRenderMode.Professional;
            statusStrip1.Size = new Size(0xb7, 0x16);
            statusStrip1.TabIndex = 12;
            statusStrip1.Text = "statusStrip1";
            toolStripStatusLabel1.BorderStyle = Border3DStyle.RaisedInner;
            toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            toolStripStatusLabel1.Size = new Size(0x4c, 0x11);
            toolStripStatusLabel1.Text = "Awaiting Login";
            tabPage2.Controls.Add(button5);
            tabPage2.Controls.Add(button4);
            tabPage2.Controls.Add(button3);
            tabPage2.Controls.Add(xtraProgressBar1);
            tabPage2.Controls.Add(listView1);
            tabPage2.Location = new Point(4, 0x16);
            tabPage2.Margin = new Padding(1);
            tabPage2.Name = "tabPage2";
            tabPage2.Padding = new Padding(3);
            tabPage2.Size = new Size(0xbd, 0xee);
            tabPage2.TabIndex = 1;
            tabPage2.Text = "Voice";
            tabPage2.UseVisualStyleBackColor = true;
            button5.Location = new Point(0x77, 0xd1);
            button5.Name = "button5";
            button5.Size = new Size(0x38, 0x17);
            button5.TabIndex = 20;
            button5.Text = "Mute";
            button5.UseVisualStyleBackColor = true;
            button5.Click += new EventHandler(button5_Click);
            button4.Location = new Point(0x41, 0xd1);
            button4.Name = "button4";
            button4.Size = new Size(0x38, 0x17);
            button4.TabIndex = 0x13;
            button4.Text = "Leave";
            button4.UseVisualStyleBackColor = true;
            button4.Click += new EventHandler(button4_Click_1);
            button3.Location = new Point(11, 0xd1);
            button3.Name = "button3";
            button3.Size = new Size(0x38, 0x17);
            button3.TabIndex = 0x12;
            button3.Text = "Talk";
            button3.UseVisualStyleBackColor = true;
            button3.Click += new EventHandler(button3_Click);
            xtraProgressBar1.Location = new Point(11, 9);
            xtraProgressBar1.Name = "xtraProgressBar1";
            xtraProgressBar1.Size = new Size(0xa4, 0x13);
            xtraProgressBar1.TabIndex = 0x11;
            xtraProgressBar1.TextBegin = "";
            xtraProgressBar1.TextContentShown = XtraProgressBar.TextShown.TextOnly;
            xtraProgressBar1.UseWaitCursor = true;
            
            listView1.Activation = ItemActivation.OneClick;
            listView1.AllowColumnReorder = true;
            listView1.Columns.AddRange(new ColumnHeader[] { columnHeader1 });
            listView1.ContextMenuStrip = contextMenuStrip1;
            listView1.HeaderStyle = ColumnHeaderStyle.None;
            listView1.HoverSelection = true;
            listView1.Location = new Point(11, 0x22);
            listView1.MultiSelect = false;
            listView1.Name = "listView1";
            listView1.Size = new Size(0xa4, 170);
            listView1.TabIndex = 13;
            listView1.UseCompatibleStateImageBehavior = false;
            listView1.View = View.Details;
            listView1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged);
            listView1.AutoArrange = true;
            listView1.CheckBoxes = true;
            listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
            columnHeader1.Width = 0x71;
            timer1.Interval = 0x1388;
            timer1.Tick += new EventHandler(timer1_Tick);
            BottomToolStripPanel.Location = new Point(0, 0);
            BottomToolStripPanel.Name = "BottomToolStripPanel";
            BottomToolStripPanel.Orientation = Orientation.Horizontal;
            BottomToolStripPanel.RowMargin = new Padding(3, 0, 0, 0);
            BottomToolStripPanel.Size = new Size(0, 0);
            TopToolStripPanel.Location = new Point(0, 0);
            TopToolStripPanel.Name = "TopToolStripPanel";
            TopToolStripPanel.Orientation = Orientation.Horizontal;
            TopToolStripPanel.RowMargin = new Padding(3, 0, 0, 0);
            TopToolStripPanel.Size = new Size(0, 0);
            RightToolStripPanel.Location = new Point(0, 0);
            RightToolStripPanel.Name = "RightToolStripPanel";
            RightToolStripPanel.Orientation = Orientation.Horizontal;
            RightToolStripPanel.RowMargin = new Padding(3, 0, 0, 0);
            RightToolStripPanel.Size = new Size(0, 0);
            LeftToolStripPanel.Location = new Point(0, 0);
            LeftToolStripPanel.Name = "LeftToolStripPanel";
            LeftToolStripPanel.Orientation = Orientation.Horizontal;
            LeftToolStripPanel.RowMargin = new Padding(3, 0, 0, 0);
            LeftToolStripPanel.Size = new Size(0, 0);
            ContentPanel.Size = new Size(0xb3, 0xb9);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0xbd, 0x109);
            base.Controls.Add(tabControl1);
            base.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            base.Name = "Form2";
            Text = "Kazoo Alpha";
            base.TopMost = true;
            base.TransparencyKey = SystemColors.ActiveBorder;
            contextMenuStrip1.ResumeLayout(false);
            tabControl1.ResumeLayout(false);
            tabPage1.ResumeLayout(false);
            tabPage1.PerformLayout();
            statusStrip1.ResumeLayout(false);
            statusStrip1.PerformLayout();
            tabPage2.ResumeLayout(false);
            base.ResumeLayout(false);
        }
        private void denable()
        {
            Hook.Enabled = !(Hook.Enabled);
        }
        private void keyuser(string data)
        {
            xtraProgressBar1.TextBegin = data + " ";
        }

        private void leveler(int i)
        {
            xtraProgressBar1.BarValue = Convert.ToInt32((double) (((double) i) / 2.5));
        }

        private void leveler3(int i)
        {
            button3.Text = Convert.ToString(i); ;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Joinattempts < 10)
            {
                lock (client)
                {
                    Joinattempts++;
                    client.JoinRoom(textBox3.Text);
                    statusStrip1.Invoke(dssd3, new object[] { "Join attempt:" + Joinattempts.ToString() });

                }
            }
            else
            {
                timer1.Stop();
                Joinattempts = 0;
                statusStrip1.Invoke(dssd3, new object[] { "Join attempt:Failed" });
            }
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
        }

        private void unignoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.SelectedItems[0].Checked = false;
            wd.unignoreuser(listView1.SelectedItems[0].Text);
        }

        private void updateStatus(string data)
        {
            statusStrip1.Items[0].Text = data;
        }

        private void wd_AddUser(string data)
        {
            listView1.Invoke(ds, new object[] { data });
        }

        private void wd_DelUser(string data)
        {
            listView1.Invoke(dss, new object[] { data });
        }

        private void wd_KeyUser(string data)
        {
            xtraProgressBar1.Invoke(dssd, new object[] { data });
        }

        private void wd_LevelUser(int data)
        {
            xtraProgressBar1.Invoke(level, new object[] { data });
        }
        private delegate void ButtonGrey();
        private delegate void Tablist(int tab);

        private delegate void Userlist(string user);
    }
}

